class Login {

    constructor(userNameElementQuerySelecter, passWordElementQuerySelecter, submitElementQuerySelecter) {
        this.userNameEle = $(userNameElementQuerySelecter);
        this.passWordEle = $(passWordElementQuerySelecter);
        this.submitEle = $(submitElementQuerySelecter);
        this.passWord = this.passWordEle.val();
        this.userName = this.userNameEle.val();
        this.csrf_token = $('#csrf').attr('content');
    this.onUserNameInput = (event) => {
        this.userName = this.userNameEle.val();
    };

    this.onPassWordInput = (event) => {
        this.passWord = this.passWordEle.val();
    };

    this.onSubmitClick = (event) => {
        if (!this.processForm()) {
            swal({
                    'title': '错误',
                    'text': '请填写完整的用户名密码',
                    'type': 'error'
                }
            );
        } else {
            this.submitEle.attr('disabled','true');
            this.submitEle.css('background-color',"#787878");
            $.ajax({
                url:'/cms/login/',
                type:"POST",
                data:{
                    'username':this.userName,
                    'password':this.passWord,
                    'csrf_token':this.csrf_token
                },
                success:(data)=>{
                    if(data.code === 200){
                        swal({
                                'title': '登录成功',
                                'text': '2秒后跳转到首页',
                                'type': 'success'
                            }
                        );
                        setInterval(()=>{
                            window.location.href = data.message
                        },2000)
                    }else{
                        swal({
                                'title': '错误',
                                'text': data.message,
                                'type': 'error'
                            }
                        );
                        this.submitEle.removeAttr('disabled');
                        this.submitEle.css('background-color',"#f5a623");
                    }

                },
                error:(error)=>{
                    console.log(error);
                    swal({
                            'title': '网络错误',
                            'text': '请检查网络设置',
                            'type': 'error'
                        }
                    );
                    this.submitEle.removeAttr('disabled');
                    this.submitEle.css('background-color',"#f5a623");
                }
            });


        }
    };
    }

    addEventListener() {
        this.userNameEle.on('input', this.onUserNameInput);
        this.passWordEle.on('input', this.onPassWordInput);
        this.submitEle.on('click',this.onSubmitClick);

    }

    processForm() {
        let userNameLength = this.userName.length;
        let passWordLength = this.passWord.lenght;
        return !(userNameLength === 0 || passWordLength === 0);

    }

}